package com.motong.gongdan.util;


import java.util.Optional;

/**
 * @Auther: FreddieFang
 * @Date: 2021/11/12 9:03
 * @Description:
 */
public class LoginUtil {
    private static ThreadLocal<Integer>   CURRENT_USER_ID          = new ThreadLocal<Integer>();

    private static ThreadLocal<String> CURRENT_ACCOUNT_USERNAME = new ThreadLocal<String>();

    private static ThreadLocal<String>       CURRENT_USER_TOKRN       = new ThreadLocal<String>();

    private static ThreadLocal<Integer>       CURRENT_USER_ROLE       = new ThreadLocal<Integer>();

    private static ThreadLocal<Integer> USER_FACTORY_ID = new ThreadLocal<Integer>();

    private static ThreadLocal<Boolean>    IGNORE_FACTORY       = new ThreadLocal<>();

    public static Integer getLoginUserRole() {
        return (Integer) CURRENT_USER_ROLE.get();
    }


    public static void setLoginUserRole(Integer role) {
        CURRENT_USER_ROLE.set(role);
    }


    public static Integer getLoginUserId() {
        return (Integer) CURRENT_USER_ID.get();
    }

    public static String getLoginUserToken() {
        return (String) CURRENT_USER_TOKRN.get();
    }


    public static void setLoginUserToken(String userToken) {
        CURRENT_USER_TOKRN.set(userToken);
    }



    public static void setLoginUserId(Integer userId) {
        CURRENT_USER_ID.set(userId);
    }

    public static void removeLoginUserId() {
        CURRENT_USER_ID.remove();
    }


    public static String getLoginAccount() {
        return (String) CURRENT_ACCOUNT_USERNAME.get();
    }

    public static void setLoginAccount(String account) {
        CURRENT_ACCOUNT_USERNAME.set(account);
    }

    public static void removeLoginAccount() {
        CURRENT_ACCOUNT_USERNAME.remove();
    }

    public static Integer getLoginUserFactory(){
        return (Integer) USER_FACTORY_ID.get();
    }

    public static void setLoginUserFactory(Integer factoryId){
        USER_FACTORY_ID.set(factoryId);
    }
    public static void removeLoginUserFactory() {
        USER_FACTORY_ID.remove();
    }

    public static Boolean getIgnoreFactory(){
        return  Optional.ofNullable(IGNORE_FACTORY.get()).orElse(false);
    }

    public static void setIgnoreFactory(Boolean ignoreFactory){
        IGNORE_FACTORY.set(ignoreFactory);
    }

    public static void removeIgnoreFactory() {
        IGNORE_FACTORY.remove();
    }

    public static void removeAll(){
        CURRENT_USER_ID.remove();
        CURRENT_ACCOUNT_USERNAME.remove();
        CURRENT_USER_TOKRN.remove();
        CURRENT_USER_ROLE.remove();
        USER_FACTORY_ID.remove();
        IGNORE_FACTORY.remove();
    }
}
